package com.imast.android.bys.ui.fragment;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.google.android.gms.internal.lv;
import com.imast.android.bys.BySApplication;
import com.imast.android.bys.R;
import com.imast.android.bys.adapter.CarroCompraAdapter;
import com.imast.android.bys.io.model.CarroCompraItem;
import com.imast.android.bys.ui.DireccionActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CarritoCompraFragment extends ListFragment {
	
	private CarritoCompraHelper helper;
	private int contador = 0;
	private CarroCompraAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	@SuppressLint("NewApi")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		BySApplication app = (BySApplication) getActivity().getApplicationContext();
		
		adapter = new CarroCompraAdapter(getActivity(), app.getLstCompra());
		
		LayoutInflater inflater = getActivity().getLayoutInflater();
		ViewGroup header = (ViewGroup)inflater.inflate(R.layout.fragment_carrito, getListView(), false);
		getListView().addFooterView(header);
		
		setListAdapter(adapter);
		
		double montoTotal = 0;
		
		for (int i=0; i < getListAdapter().getCount(); i++){
			CarroCompraItem item = (CarroCompraItem)getListAdapter().getItem(i);
			
			montoTotal += item.getProducto().getPrecio() * item.getCantidad();
			
		}
		
		TextView t = (TextView) header.findViewById(R.id.txtMontoTotal);
		BigDecimal bd = new BigDecimal(montoTotal);
	    bd = bd.setScale(2, RoundingMode.HALF_UP);
	    
		t.setText("Monto Total : " + bd.doubleValue());
		
		
		ListView listView = getListView();
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
			
			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				contador = 0;
				return false;
			}
			
			@Override
			public void onDestroyActionMode(ActionMode mode) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
		        inflater.inflate(R.menu.carro_context_menu, menu);
		        return true;
			}
			
			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
	            case R.id.action_delete:
	                deleteItems(mode);
	                mode.finish(); // Action picked, so close the CAB
	                return true;
	            default:
	                return false;
				}
			}
			
			@Override
			public void onItemCheckedStateChanged(ActionMode mode, int position,
					long id, boolean checked) {
				
				if(checked){
					contador++;
				}
				else{
					contador--;
					if(contador < 0)
						contador = 0;
					
				}
				
				mode.setTitle(contador + "Item(s) Seleccionado(s)");
				
			}
		});
		
	}
	
	@SuppressLint("NewApi")
	private void deleteItems(ActionMode mode){
		SparseBooleanArray sparseBooleanArray = getListView().getCheckedItemPositions();

        for(int i = sparseBooleanArray.size() -1; i >= 0; i--){
        	BySApplication app = (BySApplication) getActivity().getApplicationContext();
        	app.deleteItem(sparseBooleanArray.keyAt(i));
        }
            

        adapter.notifyDataSetChanged();
        mode.finish();
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		
		inflater.inflate(R.menu.carrito, menu);		
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case R.id.action_place: 
            Intent intent = new Intent(getActivity(), DireccionActivity.class);
            startActivity(intent);
            return true; 
        default: 
            return super.onOptionsItemSelected(item);
		}
	}

}
